package com.pinkpig.excel.ext.merge;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.merge.AbstractMergeStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;


/**
 * 灵活的合并策略
 * @author fengs
 */
public class FlexibleAbsoluteMergeStrategy extends AbstractMergeStrategy {

    private FlexibleMergeIndex flexibleMergeIndex;

    public FlexibleAbsoluteMergeStrategy(FlexibleMergeIndex flexibleMergeIndex) {
        if (null ==flexibleMergeIndex) {
            throw new IllegalArgumentException("All parameters must not be null");
        }
        this.flexibleMergeIndex = flexibleMergeIndex;
    }

    @Override
    protected void merge(Sheet sheet, Cell cell, Head head, Integer relativeRowIndex) {
        if (cell.getRowIndex() == flexibleMergeIndex.getFirstRowIndex() && cell.getColumnIndex() == flexibleMergeIndex.getFirstColumnIndex() &&
                (flexibleMergeIndex.getFirstRowIndex()!=flexibleMergeIndex.getLastRowIndex() || flexibleMergeIndex.getFirstColumnIndex()!=flexibleMergeIndex.getLastColumnIndex())) {
            CellRangeAddress cellRangeAddress =
                new CellRangeAddress(flexibleMergeIndex.getFirstRowIndex(), flexibleMergeIndex.getLastRowIndex(), flexibleMergeIndex.getFirstColumnIndex(), flexibleMergeIndex.getLastColumnIndex());
            sheet.addMergedRegionUnsafe(cellRangeAddress);
            //设置单元格合并后居中
            CellStyle style = cell.getCellStyle();
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            cell.setCellStyle(style);

        }
    }

    public static class FlexibleMergeIndex{
        private int firstRowIndex;
        private int lastRowIndex;
        private int firstColumnIndex;
        private int lastColumnIndex;
        public FlexibleMergeIndex(int firstRowIndex, int lastRowIndex, int firstColumnIndex, int lastColumnIndex){
            this.firstRowIndex = firstRowIndex;
            this.lastRowIndex = lastRowIndex;
            this.firstColumnIndex = firstColumnIndex;
            this.lastColumnIndex = lastColumnIndex;
        }

        public int getFirstRowIndex() {
            return firstRowIndex;
        }

        public void setFirstRowIndex(int firstRowIndex) {
            this.firstRowIndex = firstRowIndex;
        }

        public int getLastRowIndex() {
            return lastRowIndex;
        }

        public void setLastRowIndex(int lastRowIndex) {
            this.lastRowIndex = lastRowIndex;
        }

        public int getFirstColumnIndex() {
            return firstColumnIndex;
        }

        public void setFirstColumnIndex(int firstColumnIndex) {
            this.firstColumnIndex = firstColumnIndex;
        }

        public int getLastColumnIndex() {
            return lastColumnIndex;
        }

        public void setLastColumnIndex(int lastColumnIndex) {
            this.lastColumnIndex = lastColumnIndex;
        }

        public void clean(){
            this.firstRowIndex = -1;
            this.lastRowIndex = -1;
            this.firstColumnIndex = -1;
            this.lastColumnIndex = -1;
        }
    }
}
